import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'

const routes: Array<RouteRecordRaw> = [
  {path: '/',redirect: '/login'},
  {
    path: '/login',
    name: 'login',
    component: () => import( '../views/LoginView.vue')
  },
  {
    path: '/admin/home',
    name: 'adminHome',
    component: () => import('../views/admin/HomeView.vue'),
    children:[
      {
        path:"/admin/user",
        name:"userManagement",
        meta:{
          isShow: true ,
          title:"用户管理"
        },
        component: () => import( '../views/admin/UserView.vue')
      },
      {
        path:"/admin/role",
        name:"roleManagement",
        meta:{
          isShow: true ,
          title:"角色管理"
        },
        component: () => import( '../views/admin/RoleView.vue')
      },
      {
        path:"/admin/permission",
        name:"permissionManagement",
        meta:{
          isShow: false ,
          title:"权限管理"
        },
        component: () => import( '../views/admin/PermissionView.vue')
      },
      {
        path:"/admin/article",
        name:"articleManagement",
        meta:{
          isShow: true ,
          title:"文章管理"
        },
        component: () => import( '../views/admin/ArticleView.vue')
      },
      {
        path:"/admin/talk",
        name:"talkManagement",
        meta:{
          isShow: true ,
          title:"说说管理"
        },
        component: () => import( '../views/admin/TalkView.vue')
      },
      {
        path:"/admin/comment",
        name:"commentManagement",
        meta:{
          isShow: true ,
          title:"评论管理"
        },
        component: () => import( '../views/admin/CommentView.vue')
      }
    ]
  },
  {
    path: '/user/home',
    name: 'userHome',
    meta:{title:"主页"},
    component: () => import('../views/user/HomeView.vue'),
    children:[
      {
        path:"/user/article",
        name:"articleList",
        meta:{
          isShow: false ,
          title:"文章详情"
        },
        component: () => import( '../views/user/ArticleDetailsView.vue')
      },
      {
        path:"/user/talk",
        name:"talkList",
        meta:{
          isShow: false ,
          title:"说说详情"
        },
        component: () => import( '../views/user/TalkDetailsView.vue')
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
/* 路由守卫 */
router.beforeEach((to,from,next) => {
  const token: string | null = localStorage.getItem('token')
  if(!token && to.path !== '/login'){
    next('/login')
  }else{
    next()
  }
})
export default router
